// 静态属性，样式属性，剥离数据 多种屏幕尺寸都事先部署好
import echarts from 'echarts'
const staticOption = {
  color: ['#fe6e80', '#ffa286', '#ffdd89', '#a7eb87', '#2bd3bd', '#0099ff', '#7574ee'],
  tooltip: {
    trigger: 'item'
  },
  grid: {
    left: '3%',
    right: '5%',
    bottom: '5%',
    top: '20%',
    containLabel: true
  },
  xAxis: [
    {
      type: 'category',
      data: [],
      axisLabel: {
        fontSize: 18,
        color: '#cccccc',
        interval: 0,
        rotate: 25,
        margin: 20,
        padding: [40, -40, 0, 0]
      },
      axisTick: {
        show: false
      },
      axisLine: {
        lineStyle: {
          color: '#496dc8',
          width: 2
        }
      },
      splitLine: {
        show: false
      }
    }
  ],
  yAxis: [
    {
      type: 'value',
      axisLabel: {
        fontSize: 18,
        color: '#cccccc'
      },
      axisTick: {
        show: false
      },
      axisLine: {
        lineStyle: {
          color: '#496dc8',
          width: 2
        }
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(203, 216, 228, 0.2)',
          width: 2
        }
      }
    }
  ],
  series: [
    {
      name: '',
      type: 'bar',
      smooth: true,
      barMaxWidth: '30%',
      label: {
        show: true,
        position: 'top',
        fontSize: 18
      },
      itemStyle: {
        barBorderRadius: [5, 5, 0, 0]
      },
      data: []
    }
  ]
}

const staticOptionMid = {
  color: ['#fe6e80', '#ffa286', '#ffdd89', '#a7eb87', '#2bd3bd', '#0099ff', '#7574ee'],
  tooltip: {
    trigger: 'item'
  },
  grid: {
    left: '3%',
    right: '5%',
    bottom: '0',
    top: '20%',
    containLabel: true
  },
  xAxis: [
    {
      type: 'category',
      data: [],
      axisLabel: {
        fontSize: 14,
        color: '#cccccc',
        interval: 0,
        rotate: 25,
        padding: [40, -30, 0, 0]
      },
      axisTick: {
        show: false
      },
      axisLine: {
        lineStyle: {
          color: '#496dc8',
          width: 2
        }
      },
      splitLine: {
        show: false
      }
    }
  ],
  yAxis: [
    {
      type: 'value',
      axisLabel: {
        fontSize: 14,
        color: '#cccccc'
      },
      axisTick: {
        show: false
      },
      axisLine: {
        lineStyle: {
          color: '#496dc8',
          width: 2
        }
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(203, 216, 228, 0.2)',
          width: 2
        }
      }
    }
  ],
  series: [
    {
      name: '',
      type: 'bar',
      smooth: true,
      barMaxWidth: '30%',
      label: {
        show: true,
        position: 'top',
        fontSize: 12
      },
      itemStyle: {
        barBorderRadius: [5, 5, 0, 0]
      },
      data: []
    }
  ]
}

// 备选颜色，有需求则进行循环

const colors = ['#fe6e80', '#ffa286', '#ffdd89', '#a7eb87', '#2bd3bd', '#0099ff', '#7574ee']
let colorIndex = 0

// 获取设备宽度
const screen = window.screen.width

class ChartFactory {
  constructor (additionalOptions) {
    // 根据屏幕宽度获取不同配置的echarts配置
    this.option = staticOption
    if (screen < 1920 && screen > 1200) {
      this.option = staticOptionMid
    }
    // 下方复制属于浅拷贝，指向同个内存，针对可能有更改的需求，要进行一次清理
    this.clear()
    colorIndex = 0
    // // 装配数据 必须使用箭头函数
    this.option.xAxis[0].data = additionalOptions.xAxisData
    additionalOptions.seriesData.forEach((p1) => {
      this.option.series[0].data.push({
        value: p1,
        label: {
          color: colors[colorIndex]
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
            // 0% 处的颜色
            offset: 0,
            color: colors[colorIndex],
            opacity: 0
          }, {
            // 100% 处的颜色
            offset: 1,
            color: colors[colorIndex]
          }], false)
        }
      })
      colorIndex += 1
      if (colorIndex === 7) {
        colorIndex = 0
      }
    })
  }

  // 清理方法，要将部署的新数据全部置空
  clear () {
    this.option.series[0].data = []
  }

  getOption () {
    return this.option
  }
}

export { ChartFactory }
